Webcam.set({
    width: 320,
    height: 240,
    dest_width: 640,
    dest_height: 480,
    image_format: 'jpeg',
    jpeg_quality: 90
});
Webcam.attach( '#camera' );

var FPS = 0.1;
var is_stop = 0;
var stopped = 0;

Webcam.on('live', function () {
    _doSnap();
});

function update_client_info() {
    $.get('index.php?action=client_info', function (status) {
        if (status == 2) {
            is_stop = 1;
        } else {
            is_stop = 0;
            stopped && _doSnap();
        }
        setTimeout(update_client_info, 10);
    });
}


function _doSnap()
{
    Webcam.snap( function(data_uri) {
        document.getElementById('results').src=data_uri;
        var name = document.getElementById('name').value;
        Webcam.upload(data_uri, 'index.php?action=upload&name='+name, function (code, text) {
            var resp = JSON.parse(text);
            FPS = resp.FPS;
            is_stop = resp.is_stop;
            if (!is_stop) {
                setTimeout(_doSnap, 1000/FPS);
                stopped = 0;
            } else {
                stopped = 1;
            }
        })
    } );
}